package com.imc.api.shopmall.dao;

import com.imc.api.shopmall.pojo.ShopMall;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

/**
 * Created by tanweiping on 2018/9/25.
 */
@Repository
public interface ShopMallRepository extends JpaRepository <ShopMall, Integer>, JpaSpecificationExecutor <ShopMall> {

    /*
     * YT  2018/10/28 21:54
     * 方法描述：根据登陆名称获取商城信息
     */
    @Query(value = "SELECT * FROM t_shopmall shopmall " +
            "LEFT JOIN t_user u ON u.id = shopmall.userId " +
            " WHERE u.loginName=:loginName " +
            " AND shopmall.status = 0 ", nativeQuery = true)
    List <ShopMall> findAllByLoginName(@Param("loginName") String loginName);

    /*
     * YT  2018/10/28 21:56
     * 方法描述：清除商城
     */
    @Modifying
    @Query(value = "DELETE shopmall.* FROM  t_shopmall shopmall left join t_user u ON u.id = shopmall.userId WHERE u.loginName=:loginName", nativeQuery = true)
    void cleanShopMall(@Param("loginName") String loginName);

    /*
     * YT  2018/10/28 21:59
     * 方法描述：查询数量是否存在
     */
    @Query(
            value = "SELECT shopmall.* FROM t_shopmall shopmall  WHERE shopmall.userId=:userId AND shopmall.skuId=:skuId AND shopmall.status = 0 ",
            nativeQuery = true
    )
    List <ShopMall> countExists(@Param("userId") String userId, @Param("skuId") String skuId);

    /*
     * YT  2018/10/28 22:00
     * 方法描述：查询所有的用户通过用户ID和状态
     */
    List <ShopMall> findAllByUserIdAndIdIn(String userId, List <Integer> status);

    /*
     *  YT  2018/10/28 22:00
     * 方法描述：
     */
    @Modifying
    @Query(
            value = "DELETE shopmall.* FROM  t_shopmall shopmall left join t_user u ON u.id = shopmall.userId" +
                    " WHERE u.loginName=:loginName and shopmall.skuId in (:skuIds)",
            nativeQuery = true)
    void cleanShopMallByOrder(@Param("loginName") String loginName, @Param("skuIds") Set <String> skuIds);

}
